- [Instructor] When you perform a calculation,
MATLAB assigns the result to a variable,
and displays the result.
You can control that display
by changing the format of the output,
adding one or more lines to the output,
and by suppressing the output entirely.
I will show you how to do all of those tasks in this movie.
I have started MATLAB,
and I have a blank command window.
First, I'm going to start by defining a variable
that I'll use in various calculations.
I will type in radius, R-A-D-I-U-S equal 33.
So the radius of the circles I'll deal with are 33.
And Enter.
And you see that MATLAB displayed the name
of the variable and also its value.
If I wanted to suppress output,
I could add a semicolon to the end of the line.
So let's say that I want to find the circumference
of the circle.
That would be circ, C-I-R-C
is the variable name to which I will assign
the result of my calculation.
So two and asterisk for multiplication
Pi multiplied by
the radius,
so two Pi R.
I don't want the value to display,
so I will type a semicolon at the end of the line
and press Enter,
and you can see that MATLAB doesn't display it.
If I do want to see the value of the variable,
I can just type C-I-R-C,
no semicolon, Enter, and it's displayed.
You can also display the value of a variable
using the display or D-I-S-P function.
So for example, if I do D-I-S-P
left parentheses, radius,
right parentheses, Enter,
then I get the value of 33,
but it doesn't display the variable name
because that is in the statement above it.
You can also use display to add vertical space
to your output, and you can do that by adding blanks.
So I will type D-I-S-P again, display,
left parentheses,
and then I will use the blanks function,
and let's say I want to generate three blank lines.
I'll type a left parentheses, the number three,
and then a right parentheses.
Now blanks are generated in horizontal form,
in other words, on the same line.
So what I want to do is take the transpose
of this three unit horizontal vector,
converting it from a row vector to a column vector.
So I'll type the transpose symbol,
which is an apostrophe.
Follow that with a right parentheses to close out
the display function's argument list,
Enter,
and you see that I get three blank lines.
It may not seem like much,
but when you are formatting your output,
it's actually very handy.
Finally, you can format your output
using the fprintf function.
Fprintf displays values on the screen,
but it doesn't write them to a string.
There is a separate function for that
that I cover elsewhere in the course.
So let's say that I want to display
the circumference of the circle as a decimal number.
To do that I can type fprintf
followed by a left parentheses then a single quote,
so I'm starting a string.
The circumference
is followed by a colon,
then a space.
Now I need to indicate that I want to have
a decimal number displayed,
so I will type a percent sign
followed by the letter D.
And that is the indicator that I want
a decimal number to go there.
Then I'll type a period,
and then I want to have a new line added.
That's another special character,
so I will type a backslash
followed by an N,
and remember, this is all still within the string,
so I will close it with a single quote,
or apostrophe, comma,
and then the circumference is C-I-R-C.
Type a right parentheses and Enter,
and there you see that I get the result.
I can also display it as a floating point number,
and for that I would use the exact same function,
except that I would use F instead of D
after the percent sign.
So fprintf, left parentheses,
apostrophe, the circumference
is colon
percent F period
followed by a backslash then N for the new line.
Close out the string with an apostrophe or single quote,
comma, C-I-R-C,
Enter,
and now you see that instead of scientific notation,
I get the actual floating point decimal number.
Two other very useful codes that you can use
are percent C for one character,
and percent S for an entire string.
As you can see, by using these various techniques,
you have a lot of control over your output.